package com.js.sys.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import androidx.annotation.Nullable;

import com.google.gson.Gson;
import com.huawei.hms.hmsscankit.ScanUtil;
import com.huawei.hms.ml.scan.HmsScan;
import com.js.lib_common.mvp.model.response.BindBean;
import com.js.lib_common.mvp.model.response.LoginResponse;
import com.js.lib_common.utils.SPUtils;
import com.js.sys.R;
import com.js.sys.fragment.MainFragment;
import com.js.sys.fragment.MainFragment2;
import com.js.sys.fragment.MainFragment3;

import org.greenrobot.eventbus.EventBus;

import java.text.SimpleDateFormat;
import java.util.Locale;

import me.yokeyword.fragmentation.SupportActivity;
import me.yokeyword.fragmentation.anim.DefaultHorizontalAnimator;
import me.yokeyword.fragmentation.anim.FragmentAnimator;

/**
 * 入口
 * Created by wz on 20/3/31.
 */
public class MainActivity extends SupportActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String json = (String) SPUtils.get(this, "LoginResponse", "");
        Gson gson = new Gson();
        LoginResponse bean = gson.fromJson(json, LoginResponse.class);
        switch (bean.getRoleType()) {
            case 1:
                loadRootFragment(R.id.fl_container, MainFragment.newInstance());
                break;
            case 2:
                loadRootFragment(R.id.fl_container, MainFragment2.newInstance());
                break;

            case 3:
                loadRootFragment(R.id.fl_container, MainFragment3.newInstance());
                break;
        }


    }

    @Override
    public void onBackPressedSupport() {
        // 对于 4个类别的主Fragment内的回退back逻辑,已经在其onBackPressedSupport里各自处理了
        super.onBackPressedSupport();
    }

    @Override
    public FragmentAnimator onCreateFragmentAnimator() {
        // 设置横向(和安卓4.x动画相同)
        return new DefaultHorizontalAnimator();
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        //receive result after your activity finished scanning
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK || data == null) {
            return;
        }
        // Obtain the return value of HmsScan from the value returned by the onActivityResult method by using ScanUtil.RESULT as the key value.
        if (requestCode == BindBean.REQUEST_CODE_BIND
                | requestCode == BindBean.REQUEST_CODE_CHECK
                || requestCode == BindBean.REQUEST_CODE_CHECK_IN
                || requestCode == BindBean.REQUEST_CODE_CHECK_OUT
                || requestCode == BindBean.REQUEST_CODE_DEFAULT_WORK
                || requestCode == BindBean.REQUEST_CODE_CHECK_OUT_CUSTOM
        ) {
            Object obj = data.getParcelableExtra(ScanUtil.RESULT);
            if (obj instanceof HmsScan) {
                String msg = ((HmsScan) obj).getOriginalValue();
                if (!TextUtils.isEmpty(msg)) {
                    SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.CHINA);
                    String time = sDateFormat.format(new java.util.Date());
                    EventBus.getDefault().post(new BindBean(requestCode, time, msg));
                }

            }
        }
    }


}
